package com.bishe.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.model.LoginModel;
import com.bishe.pojo.Admin;
import com.bishe.pojo.FileInfo;
import com.bishe.pojo.Student;
import com.bishe.pojo.Teacher;
import com.bishe.mapper.TeacherDao;
import com.bishe.query.FileQuery;
import com.bishe.query.TeacherQuery;
import com.bishe.service.IFileService;
import com.bishe.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.util.EcoBootException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.bishe.util.JobUtils.checkImgCode;
import static com.bishe.util.JobUtils.checkLogin;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwx
 * @since 2022-04-14
 */
@Service
public class TeacherServiceImp extends ServiceImpl<TeacherDao, Teacher> implements TeacherService {

    @Resource
    private IFileService fileService;

    @Override
    public LoginModel login(LoginModel loginModel) {
        if(loginModel ==null){
            throw new EcoBootException("登录失败!");
        }
        //验证 验证码
        checkImgCode(loginModel.getTxt());

        if (StrUtil.isEmpty(loginModel.getAccount())||StrUtil.isEmpty(loginModel.getPassWord())){
            throw new EcoBootException("用户名密码为空!");
        }

        String userName = loginModel.getAccount();
        String password = loginModel.getPassWord();
        Boolean rememberMe = loginModel.getRememberMe();
        if (rememberMe == null){
            rememberMe = false;
        }
        //根据用户名密码查找
        Teacher one = getOne(new LambdaQueryWrapper<Teacher>()
                        .eq(Teacher::getTeacherNo, userName)
                        .eq(Teacher::getPassWord, password)
                ,false);

        if (one == null) {
            return null;
        }
        StpUtil.login(one.getId(),rememberMe);

        LoginModel model = new LoginModel();
        model.setUserName(one.getName());
        model.setSatoken(StpUtil.getTokenValue());

        return model;
    }

    @Override
    public IPage<Teacher> toPage(TeacherQuery query) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getName())){
            wrapper.like(Teacher::getName, query.getName());
        }

        wrapper.orderByDesc(Teacher::getCreateTime);

        return this.page(query.toPage(), wrapper);
    }

    @Override
    public void addOrUpdate(Teacher teacher) {
        Teacher byId = this.getById(teacher);
        if (byId == null){
            teacher.setId(StrUtil.uuid());
            teacher.setCreateTime(new Date());
            save(teacher);
            return;
        }
        updateById(teacher);
    }

    @Override
    public IPage<FileInfo> myFiles(FileQuery query) {
        String loginId = StpUtil.getLoginId().toString();
        LambdaQueryWrapper<FileInfo> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getName())){
            wrapper.like(FileInfo::getName,query.getName());
        }
        wrapper.eq(FileInfo::getCreateBy,loginId);
        wrapper.orderByDesc(FileInfo::getCreateTime);
        Teacher teacher = this.getById(loginId);

        Page<FileInfo> page = fileService.page(query.toPage(), wrapper);
        for (FileInfo fileInfo : page.getRecords()) {
            fileInfo.setCreateByName(teacher.getName());
            String fileName = fileInfo.getName();
            fileInfo.setName(fileName.substring(fileName.indexOf("_")+1,fileName.lastIndexOf(".")));
        }
        return page;
    }
}
